package org.masterinftel.scrumpmt.activities;

import java.util.ArrayList;

import org.masterinftel.scrumpmt.Constants;
import org.masterinftel.scrumpmt.R;
import org.masterinftel.scrumpmt.ScrumApplication;
import org.masterinftel.scrumpmt.adapters.TasksViewPagerAdapter;
import org.masterinftel.scrumpmt.json.model.Task;
import org.masterinftel.scrumpmt.protocol.Protocol;
import org.masterinftel.scrumpmt.util.UtilNetwork;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;

import com.jakewharton.android.viewpagerindicator.TitlePageIndicator;

public class TasksActivity extends Activity {

	private TasksViewPagerAdapter mAdapter;
	private ViewPager mPager;
	private ScrumApplication application;
	private Context ctx;
	private String URLFrontController;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.swipping);
		setTitle("ScrumPMT - Dashboard");
		// creamos variables utiles para la aplicacion
		ctx = this;
		application = (ScrumApplication) getApplication();

		mAdapter = new TasksViewPagerAdapter(this);
		mPager = (ViewPager) findViewById(R.id.viewpager);
		TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
		mPager.setAdapter(mAdapter);
		indicator.setViewPager(mPager);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.blackboard_menu, menu);
		return true;
	}

	private void changeTask(ArrayAdapter<Task> from, ArrayAdapter<Task> to,
			int col) {
		ArrayList<Task> taskToRemove = new ArrayList<Task>();
		Task task;
		col++;
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		URLFrontController = prefs.getString(Constants.KEY_URL_PREFERENCE, "");

		for (int i = 0; i < from.getCount(); i++) {
			task = from.getItem(i);
			if (task.isSelected()) {
				task.setSelected(false);
				to.add(task);
				String urlRest = URLFrontController + ";jsessionid="
						+ application.getSessionID() + "?action="
						+ Protocol.CHANGE_ESTAD_TAREA + "&value="
						+ task.getId() + "&estad=" + col;

				UtilNetwork.getJSON(urlRest);
				taskToRemove.add(task);
			}
		}
		// Eliminamos de la lista origen las tareas que se han movido
		for (Task t : taskToRemove) {
			from.remove(t);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuTodo:
			// Pasamos las tareas marcadas en DOING y DONE a TODO
			changeTask(mAdapter.getmListDOING(), mAdapter.getmListTODO(),
					Constants.PAGE_TODO);
			changeTask(mAdapter.getmListDONE(), mAdapter.getmListTODO(),
					Constants.PAGE_TODO);
			mAdapter.instantiateItem(mPager, Constants.PAGE_TODO);
			return true;
		case R.id.menuDoing:
			// Pasamos las tareas marcadas en TODO y DONE a DOING
			changeTask(mAdapter.getmListTODO(), mAdapter.getmListDOING(),
					Constants.PAGE_DOING);
			changeTask(mAdapter.getmListDONE(), mAdapter.getmListDOING(),
					Constants.PAGE_DOING);
			mAdapter.instantiateItem(mPager, Constants.PAGE_DOING);
			return true;
		case R.id.menuDone:
			// Pasamos las tareas marcadas de DOING y TODO a DONE
			changeTask(mAdapter.getmListDOING(), mAdapter.getmListDONE(),
					Constants.PAGE_DONE);
			changeTask(mAdapter.getmListTODO(), mAdapter.getmListDONE(),
					Constants.PAGE_DONE);
			mAdapter.instantiateItem(mPager, Constants.PAGE_DONE);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

}
